package com.pctx.bibacrmrbac.service.impl;

import com.pctx.bibacommon.util.UuidUtil;
import com.pctx.bibacrmrbac.dao.PermissionOrderMapper;
import com.pctx.bibacrmrbac.pojo.Permission;
import com.pctx.bibacrmrbac.pojo.PermissionExample;
import com.pctx.bibacrmrbac.pojo.PermissionOrder;
import com.pctx.bibacrmrbac.pojo.PermissionOrderExample;
import com.pctx.bibacrmrbac.service.PermissionOrderService;
import com.pctx.bibacrmrbac.service.PermissionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.security.Permissions;
import java.util.Date;
import java.util.List;

/**
 * Created by chason on 2018/3/2.11:15
 */
@Service("permissionOrderService")
public class PermissionOrderServiceImpl implements PermissionOrderService {
    @Autowired
    private PermissionOrderMapper permissionOrderMapper;
    @Autowired
    private PermissionService permissionService;

    @Override
    public List<PermissionOrder> getByCompanyUuidOrUserUuid(String companyUuid, Byte status, Date beginTime) throws Exception {
        PermissionOrderExample example = new PermissionOrderExample();
        PermissionOrderExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(companyUuid))
            criteria.andCompanyUuidEqualTo(companyUuid);
        if (null != status)
            criteria.andStatusEqualTo(status);
        if (null != beginTime)
            criteria.andCreateTimeGreaterThanOrEqualTo(beginTime);

        return permissionOrderMapper.selectByExample(example);
    }

    @Override
    public PermissionOrder addOrder(String companyUuid, String permissionUuid, String permissionPackageUuid, BigDecimal amount, Byte payType) throws Exception {
        if (StringUtils.isEmpty(companyUuid) || StringUtils.isEmpty(permissionUuid) || StringUtils.isEmpty
                (permissionPackageUuid) || null == amount || null == payType)
            return null;

        Permission permission = permissionService.getByUuid(permissionUuid);
        if (null == permission)
            return null;

        PermissionOrder permissionOrder = new PermissionOrder();
        permissionOrder.setUuid(UuidUtil.randomUUID());
        permissionOrder.setPermissionUuid(permissionUuid);
        permissionOrder.setPermissionName(permission.getName());
        permissionOrder.setPermissionPackageUuid(permissionPackageUuid);

        return null;
    }
}
